package hw724;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/* 斗地主 */
public class zuoye8_fangfa {
    public static void main(String[] args) {
        // 创建扑克牌
        List<String> poker = new ArrayList<>();
        String[] colors = {"♠", "♣", "♦", "♥"};
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

     
        Collections.addAll(poker, "大王", "小王");

        IntStream.range(0, numbers.length)
                .forEach(i -> {
                    for (String color : colors) {
                        poker.add(color + numbers[i]);
                    }
                });

        System.out.println("生成的扑克牌: " + poker);

        // 洗牌
        Collections.shuffle(poker);
        System.out.println("洗牌后的扑克牌: " + poker);

        // 创建玩家
        zuoye8_player p1 = new zuoye8_player();
        p1.setName("dbw");
        zuoye8_player p2 = new zuoye8_player();
        p2.setName("dbw1");
        zuoye8_player p3 = new zuoye8_player();
        p3.setName("zky");

        // 摸牌
        List<String> dipai = new ArrayList<>();
        IntStream.range(0, poker.size()).forEach(i -> {
            String card = poker.get(i);
            if (i >= 51) {
                dipai.add(card); // 底牌
            } else if (i % 3 == 0) {
                p1.getPoker().add(card);
            } else if (i % 3 == 1) {
                p2.getPoker().add(card);
            } else {
                p3.getPoker().add(card);
            }
        });

        // 创建 HashMap 存储玩家和他们的牌
        HashMap<String, ArrayList<String>> map = new HashMap<>();
        map.put(p1.getName(), p1.getPoker());
        map.put(p2.getName(), p2.getPoker());
        map.put(p3.getName(), p3.getPoker());

        // 牌的排序
        map.forEach((playerName, playerPoker) -> {
            sortPoker(playerPoker);
            System.out.println(playerName + "的牌: " + playerPoker);
        });

        System.out.println("底牌: " + dipai);
    }

    // 使用方法引用对牌进行排序
    private static void sortPoker(ArrayList<String> playerPoker) {
        playerPoker.sort(zuoye8_fangfa::comparePoker); // 使用方法引用
    }

    // 比较两张牌的等级
    private static int comparePoker(String o1, String o2) {
        return getRank(o1) - getRank(o2);
    }

    // 获取牌的等级
    private static int getRank(String card) {
        String numberPart = card.replaceAll("[♠♣♦♥]", ""); // 去掉花色
        if (numberPart.equals("小王")) return 15;
        if (numberPart.equals("大王")) return 16;
        String[] ranks = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        for (int i = 0; i < ranks.length; i++) {
            if (numberPart.equals(ranks[i])) return i;
        }
        return 0; 
    }
}